import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:untitled/utils/common_toast.dart';

import '../routes.dart';
import '../utils/dio_http.dart';
import '../utils/string_is_null_or_empty.dart';

/*
 * 注册页
 */
class RegisterPage extends StatefulWidget {
  const RegisterPage({Key key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var repeatPasswordController = TextEditingController();

  //注册请求
  _registerHandler() async {
    var userName = usernameController.text;
    var password = passwordController.text;
    var repeatPassword = repeatPasswordController.text;
    //判断密码和确认密码是否一致
    if(repeatPassword != password){
      CommonToast.showToast("两次输入的密码不一致");
      return;
    }
    if(stringIsNullOrEmpty(userName) || stringIsNullOrEmpty(password)){
      CommonToast.showToast("用户名或密码不能为空");
      return;
    }
    const url = '/user/registered';
    var params = {"username":userName,"password":password};
    //网络请求返回结果
    var res = await DioHttp.of(context).post(url,params);
    //解析返回结果
    var resString = json.decode(res.toString());
    int status = resString['status'];
    String description = resString['description']??"内部错误";
    CommonToast.showToast(description);
    //如果返回状态为2开头我们就认为登录成功
    if(status.toString().startsWith('2')){
      Navigator.of(context).pushReplacementNamed('login');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('注册'),
      ),
      body: SafeArea(
        minimum: EdgeInsets.all(20), //上下左右边距为20
        child: ListView(
          children: [
            TextField(
                controller: usernameController,
                decoration:
                    InputDecoration(labelText: "用户名", hintText: "请输入用户名")),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: passwordController,
              decoration: InputDecoration(labelText: '密码', hintText: '请输入密码'),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: repeatPasswordController,
              decoration:
                  InputDecoration(labelText: '确认密码', hintText: '请再次输入密码'),
            ),
            SizedBox(
              height: 10,
            ),
            ElevatedButton(
              child: Text('注册'),
              onPressed: () {
                _registerHandler();
              },
              style: ElevatedButton.styleFrom(
                  primary: Colors.green, onPrimary: Colors.white),
            ),
            SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('已有账号'),
                TextButton(
                  child: Text('去登录', style: TextStyle(color: Colors.green)),
                  onPressed: () {
                    Navigator.pushReplacementNamed(context, Routes.login);
                  },
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
